package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/find-valid-matrix-given-row-and-column-sums/">1605. 给定行和列的和求可行矩阵</a>
 */
public class Solution1605 extends BaseSolution {

    public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
        int[][] rst = new int[rowSum.length][colSum.length];
        for (int i = 0; i < rowSum.length; i++) {
            for (int j = 0; j < colSum.length; j++) {
                if (colSum[j] == 0) {
                    continue;
                }
                int max = Math.min(rowSum[i], colSum[j]);
                rst[i][j] = max;
                rowSum[i] -= max;
                colSum[j] -= max;
                if (rowSum[i] == 0) {
                    break;
                }
            }
        }
        return rst;
    }

    public static void main(String[] args) {
        Solution1605 solution = new Solution1605();
    }
}